package com.fieldchain.ex.fa100.ui.trade.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.bean.TradBean;
import com.fieldchain.ex.fa100.inter.TradeCallBack;

import java.util.ArrayList;
import java.util.List;

import com.fieldchain.ex.fa100.utils.common.TimeUtils;
import com.fieldchain.ex.fa100.utils.string.NumberFormate;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/13
 */
public class TradNewAdapter extends RecyclerView.Adapter<TradNewAdapter.ViewHolder> {
    private Context mContext;
    private int c1, c2,c3;
    private TradeCallBack callBack;
    private int priceLength;
    private int numberLength;
    public TradNewAdapter(Context mContext) {
        this.mContext = mContext;
        c2 = Color.parseColor("#FFDC3950");
        c1 = Color.parseColor("#FF17AE5D");
        c3 = Color.parseColor("#00ffffff");
    }

    private List<TradBean.DealRecordBean> list = new ArrayList<>();

    public void setData(List<TradBean.DealRecordBean> list,int priceLength,int numberLength) {
        this.priceLength=priceLength;
        this.numberLength=numberLength;
        if (list == null) list = new ArrayList<>();
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    public void setCallBack(TradeCallBack callBack) {
        this.callBack = callBack;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_new_trade, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final TradBean.DealRecordBean bean = list.get(position);
        String price = NumberFormate.m(bean.getPrice(),priceLength);
        holder.tvTime.setText(TimeUtils.getSimpleTime4(bean.getDate()*1000));
        holder.tvNum.setText(NumberFormate.m(bean.getAmount(),numberLength));

        if (bean.getType() == 1) {
            holder.tvLeft.setTextColor(c1);
            holder.tvPoint.setTextColor(c1);
            holder.tvRight.setTextColor(c1);
        } else {
            holder.tvLeft.setTextColor(c2);
            holder.tvPoint.setTextColor(c2);
            holder.tvRight.setTextColor(c2);
        }

        if(price.contains(".")){
            String str[]=price.split("\\.");
            holder.tvLeft.setText(str[0]);
            if(str.length>1){
                holder.tvRight.setText(str[1]);
            }else{
                holder.tvPoint.setTextColor(c3);
            }
        }else{
            holder.tvLeft.setText(price);
            holder.tvPoint.setTextColor(c3);
        }




        holder.reaItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (callBack == null) return;
                callBack.onClick(bean.getType(), position, bean.getPrice());
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();

    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private RelativeLayout reaItem;
        private TextView tvTime;
        private TextView tvLeft;
        private TextView tvPoint;
        private TextView tvRight;
        private TextView tvNum;


        public ViewHolder(View itemView) {
            super(itemView);
            reaItem = itemView.findViewById(R.id.rea_item);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvLeft = itemView.findViewById(R.id.tv_left);
            tvPoint = itemView.findViewById(R.id.tv_point);
            tvRight = itemView.findViewById(R.id.tv_right);
            tvNum = itemView.findViewById(R.id.tv_num);
        }
    }
}
